/**
 * 
 */

var append = false;

function gotFS(fileSystem) 
{
   fileSystem.root.getFile("person.txt", {"create":true}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) 
{
   fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer)
{
  writer.onwrite = function(evt) {
            console.log("write success");
        };
        
    if(append == true)
    { 
    	writer.seek(writer.length);
    	writer.write("Name: "+document.forms.person[0].value+"\n"+"Age: "+document.forms.person[1].value+"\n"+"Weight: "+document.forms.person[2].value+"\n"+"-------------------------\n");
    }
    else
    {
    	writer.write("Name: "+document.forms.person[0].value+"\n"+"Age: "+document.forms.person[1].value+"\n"+"Weight: "+document.forms.person[2].value+"\n"+"-------------------------");
    	append = true;
    }
    //writer.write("Age: "+document.forms.person[1].value+"\n");
	//writer.write("Weight: "+document.forms.person[2].value+"\n");
	//writer.write("-------------------------");
}

function fail(error) 
{
  console.log(error.code);
}	
	
function savePerson()
{
	alert(document.forms.person[0].value);
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
	
}